package com.hskn.hss.module.employeeTrack.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.carPlan.mapper.CarPlanMapper;
import com.hskn.hss.module.casesetissuesource.entity.CasesetIssueSource;
import com.hskn.hss.module.employeeCorrectRecord.entity.EmployeeCorrectRecord;
import com.hskn.hss.module.employeeTrack.entity.EmployeeTrack;
import com.hskn.hss.module.employeeTrack.mapper.EmployeeTrackMapper;
import com.hskn.hss.module.employeeTrack.service.EmployeeTrackService;
import com.hskn.hss.module.employeeTrack.to.EmployeeTrackTo;
import com.hskn.hss.module.employeeTrack.vo.AlarmAndViolationEmployeeVo;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.grid.service.IGridService;
import com.hskn.hss.module.grid.service.impl.GridServiceImpl;
import com.hskn.hss.module.gridBindCheckWork.service.GridBindCheckWorkService;
import com.hskn.hss.module.gridCheckWorkSnapshot.to.GridBindCheckworkTo;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Slf4j
@RestController
@RequestMapping("/hss/employeeTrack")
public class EmployeeTrackController {

    @Autowired
    private EmployeeTrackService employeeTrackService;

    @Autowired
    private GridBindCheckWorkService gridBindCheckWorkService;

    @Autowired
    private IGridService gridService;

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List list = employeeTrackService.mList(params);
            return AjaxResult.success(list);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    public AjaxResult<Object> selectPage(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = employeeTrackService.selectPage(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "getEmployeeTrackGrid", method = RequestMethod.POST)
    public AjaxResult<Object> getEmployeeTrackGrid(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List list = employeeTrackService.getEmployeeTrackGrid(params);
            return AjaxResult.success(list);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "getEmployeeAlarm", method = RequestMethod.POST)
    public AjaxResult<Object> getEmployeeAlarm(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List list = employeeTrackService.getEmployeeAlarm(params);
            return AjaxResult.success(list);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "getCheckWorkGrid", method = RequestMethod.POST)
    public AjaxResult<Object> getCheckWorkGrid(@RequestBody @Validated GridBindCheckworkTo gridBindCheckworkTo) {
        try {
            List<Grid> list = gridService.gridIdList(gridBindCheckWorkService.gridIdSelect(gridBindCheckworkTo));
            return AjaxResult.success(list);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /***
     * 新增信息
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.INSERT, tableName = "employee_track", bean = EmployeeTrack.class, beanMapper = EmployeeTrackMapper.class, names = "employeeId")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public AjaxResult<Object> insert(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        return employeeTrackService.insert(params);
    }

    @ApiOperation(value = "人员轨迹报警和违规接口", notes = "报警违规")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = AlarmAndViolationEmployeeVo.class)
    })
    @PostMapping("alarmAndViolationEmployeeList")
    public AjaxResult<Object> alarmAndViolationEmployeeList(@RequestBody @Validated EmployeeTrackTo employeeTrackTo) {
        try {
            return AjaxResult.success(employeeTrackService.alarmAndViolationEmployeeList(employeeTrackTo));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

}

